#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
	int	high;
	int width;
	int bird_x,bird_y;
	int x_top,x_down,wall_y;
	int score;

void gotoxy(int x,int y)
	{

		HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
		COORD pos;
		pos.X=x;
		pos.Y=y;
		SetConsoleCursorPosition(handle,pos);
	}
void startup()
{

	high=24;
	width=40;
	bird_x=high/3,bird_y=width/6;
	x_top=rand()%(high-5),x_down=x_top+5;
	wall_y=width-3;
	score=0;

}

 
void show()
{

	gotoxy(0,0);  //清屏
	int i,j;
	for(i=0;i<=high;i++)
	{
		for(j=0;j<=width;j++)
		{
			if((i==bird_x)&&(j==bird_y))
			{
				printf("@");
			}
			else if(j==width)
			{
				printf("|");
			}
			else if(i==(high-1))
			{
				printf("_");
			}
			else if((j==wall_y)&&((i<=x_top)||(i>=x_down)))
			{
				printf("#");
			}
			else
			{
				printf(" ");
			}

		}
		printf("\n");
	}
	printf("得分：%d",score);

}

 
void upwhitoutInput()
{
	static int speed=0;
	if(speed<=2)
	{
		speed++;
	}
	else
	{
		bird_x++;
		speed=0;
	}

	if((bird_y==wall_y)&&(bird_x>x_top)&&(bird_x<x_down))
	{
		score++;
	}
	if(((bird_y==wall_y)&&((bird_x<=x_top)||(bird_x>=x_down)))||bird_x>=high)
	{
		printf("游戏结束！");
		exit(0);
		system("pause");
	}


	if(wall_y>=0)
	{
		wall_y--;
	}
	else
	{
		wall_y=width-3;
		x_top=rand()%(high-5),x_down=x_top+5;
	}
}

void upwhitInput()
{
	char input;

	if(kbhit())  //按键开始
	{

		input=getch();
		if(input=='w')
		{
			bird_x-=2;   //鸟上升
		}
		if(input=='s')
		{
			bird_x+=2;  //鸟下降
		}
		
	}

}

 
 
int main()
{
	startup();  //数据初始化
	while(1)
	{
		show();  //显示
		upwhitoutInput();  //更新无输入
		upwhitInput();  //更新有输入
	}

	return 0;
}